package com.canaan.business.service.storehouse.impl;

import java.util.Date;
import java.util.List;
import com.canaan.common.utils.DateUtils;
import com.canaan.common.utils.SecurityUtils;
import com.canaan.common.utils.TableNoUtil;
import com.canaan.common.utils.uuid.UUID;
import com.canaan.common.utils.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import javax.annotation.Resource;
import com.canaan.business.mapper.storehouse.StorehouseStockRecordMapper;
import com.canaan.business.domain.storehouse.StorehouseStockRecord;
import com.canaan.business.service.storehouse.IStorehouseStockRecordService;
import com.canaan.common.annotation.TableName;
/**
 * 库存明细纪录Service业务层处理
 *
 * @author canaan
 * @date 2025-03-30
 */
@Service
public class StorehouseStockRecordServiceImpl implements IStorehouseStockRecordService {
    @Resource
    private StorehouseStockRecordMapper storehouseStockRecordMapper;

    /**
     * 查询库存明细纪录
     *
     * @param id 库存明细纪录主键
     * @return 库存明细纪录
     */
    @Override
    public StorehouseStockRecord selectStorehouseStockRecordById(Long id) {
        return storehouseStockRecordMapper.selectStorehouseStockRecordById(id);
    }

    /**
     * 查询库存明细纪录列表
     *
     * @param storehouseStockRecord 库存明细纪录
     * @return 库存明细纪录
     */
    @Override
    public List<StorehouseStockRecord> selectStorehouseStockRecordList(StorehouseStockRecord storehouseStockRecord) {
        return storehouseStockRecordMapper.selectStorehouseStockRecordList(storehouseStockRecord);
    }

    /**
     * 新增库存明细纪录
     *
     * @param storehouseStockRecord 库存明细纪录
     * @return 结果
     */
    @Override
    public int insertStorehouseStockRecord(StorehouseStockRecord storehouseStockRecord) {
        if(StringUtils.isBlank(storehouseStockRecord.getNo())){
            storehouseStockRecord.setNo(TableNoUtil.getNo(StorehouseStockRecord.class));
        }
        storehouseStockRecord.setCreateBy(SecurityUtils.getUsername());
        storehouseStockRecord.setCreateTime(DateUtils.getNowDate());
        return storehouseStockRecordMapper.insertStorehouseStockRecord(storehouseStockRecord);
    }

    /**
     * 批量新增库存明细纪录
     *
     * @param storehouseStockRecordList 库存明细纪录列表
     * @return 结果
     */
    @Override
    public int batchInsertStorehouseStockRecord(List<StorehouseStockRecord> storehouseStockRecordList) {
        String username = SecurityUtils.getUsername();
        Date currerTime = DateUtils.getNowDate();
        for (StorehouseStockRecord storehouseStockRecord : storehouseStockRecordList){
            if(StringUtils.isBlank(storehouseStockRecord.getNo())){
                storehouseStockRecord.setNo(TableNoUtil.getNo(StorehouseStockRecord.class));
            }
            storehouseStockRecord.setCreateBy(username);
            storehouseStockRecord.setCreateTime(currerTime);
        }
        int insertNum = 0;
        for (int i=0; i<storehouseStockRecordList.size();) {
            int endIndex = i+500;
            if (endIndex > storehouseStockRecordList.size()) {
                endIndex = storehouseStockRecordList.size();
            }
            insertNum = insertNum + storehouseStockRecordMapper.batchInsertStorehouseStockRecord(storehouseStockRecordList.subList(i, endIndex));
            i = endIndex;
        }
        return insertNum;
    }

    /**
     * 修改库存明细纪录
     *
     * @param storehouseStockRecord 库存明细纪录
     * @return 结果
     */
    @Override
    public int updateStorehouseStockRecord(StorehouseStockRecord storehouseStockRecord) {
        storehouseStockRecord.setUpdateBy(SecurityUtils.getUsername());
        storehouseStockRecord.setUpdateTime(DateUtils.getNowDate());
        return storehouseStockRecordMapper.updateStorehouseStockRecord(storehouseStockRecord);
    }

    /**
     * 批量修改库存明细纪录
     *
     * @param storehouseStockRecordList 库存明细纪录列表
     * @return 结果
     */
    @Override
    public int batchUpdateStorehouseStockRecord(List<StorehouseStockRecord> storehouseStockRecordList) {
        String username = SecurityUtils.getUsername();
        Date currerTime = DateUtils.getNowDate();
        for (StorehouseStockRecord storehouseStockRecord : storehouseStockRecordList){
            storehouseStockRecord.setUpdateBy(username);
            storehouseStockRecord.setUpdateTime(currerTime);
        }
        int updateNum = 0;
        for (int i=0; i<storehouseStockRecordList.size();) {
            int endIndex = i+500;
            if (endIndex > storehouseStockRecordList.size()) {
                endIndex = storehouseStockRecordList.size();
            }
            updateNum = updateNum + storehouseStockRecordMapper.batchUpdateStorehouseStockRecord(storehouseStockRecordList.subList(i, endIndex));
            i = endIndex;
        }
        return updateNum;
    }

    /**
     * 批量删除库存明细纪录
     *
     * @param ids 需要删除的库存明细纪录主键集合
     * @return 结果
     */
    @Override
    public int deleteStorehouseStockRecordByIds(Long[] ids) {
        return storehouseStockRecordMapper.deleteStorehouseStockRecordByIds(ids);
    }

    /**
     * 删除库存明细纪录信息
     *
     * @param id 库存明细纪录主键
     * @return 结果
     */
    @Override
    public int deleteStorehouseStockRecordById(Long id) {
        return storehouseStockRecordMapper.deleteStorehouseStockRecordById(id);
    }

    /**
     * 查询库存明细纪录
     *
     * @param no 编号
     * @return 库存明细纪录
     */
    @Override
    public StorehouseStockRecord selectStorehouseStockRecordByNo(String no) {
        return storehouseStockRecordMapper.selectStorehouseStockRecordByNo(no);
    }

    /**
     * 批量删除库存明细纪录
     *
     * @param nos 需要删除的编号集合
     * @return 结果
     */
    @Override
    public int deleteStorehouseStockRecordByNos(String[] nos) {
        return storehouseStockRecordMapper.deleteStorehouseStockRecordByNos(nos);
    }

    /**
     * 删除库存明细纪录信息
     *
     * @param no 编号
     * @return 结果
     */
    @Override
    public int deleteStorehouseStockRecordByNo(String no) {
        return storehouseStockRecordMapper.deleteStorehouseStockRecordByNo(no);
    }
}
